package com.fpf.springboot.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.tomcat.util.codec.binary.Base64;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class QRCodeUtil {
	//图片存入缓存中
	public String createQRCode(String content) throws IOException {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			QRCodeWriter writer = new QRCodeWriter();
			BitMatrix bitMatix = writer.encode(content, BarcodeFormat.QR_CODE, 200, 200);
			BufferedImage bufferedImage=MatrixToImageWriter.toBufferedImage(bitMatix);
			ImageIO.write(bufferedImage, "png", os);
			return "data:image/png;base64,"+Base64.encodeBase64String(os.toByteArray());
		} catch (WriterException e) {
			e.printStackTrace();
		}finally {
			if(os!=null) {
				os.close();
			}
		}
		return null;
	}
	
}
